package main;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
import connection.DataTransfer;
import connection.IMessageListenner;
import utils.Debug;

/**
 *
 * @author daz
 */
public class Request {

    private DataTransfer dataTransfer;

    public void initializeConnection(String host, String port) {
        dataTransfer = new DataTransfer(host, port);
    }

    public void setMessageListenner(IMessageListenner messageListenner) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.setMessageListenner(messageListenner);
    }

    public void setMyName(String name) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.setMyName(name);
    }

    public void chosseGame(int gameType) {
        Debug.d("request choosse game:" + dataTransfer);
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("GA|" + gameType);
    }

    public void login(String userName, String password) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("LO|" + userName + "|" + password);
    }

    public void fnMessgePhom() {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("FN|");
    }

    public void finishPhom(int idWind) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("CT|" + idWind);
    }

    public void finishPhom(int[] cardIds, int numberCard) {
        if (dataTransfer == null) {
            return;
        }
        StringBuilder msg = new StringBuilder();
        msg.append("SCO");
        msg.append("|");
        for (int i = 0; i < numberCard; i++) {
            msg.append(cardIds[i]);
            if (i != numberCard - 1) {
                msg.append("|");
            }
        }
        dataTransfer.request(msg.toString());
    }

    public void register(String userName, String password, int gender) {
        if (dataTransfer == null) {
            return;
        }
//        dataTransfer.request("REGISTER|" + userName + "|" + password + "|" + gender + "|" + Constants.VERSION + "|" + Constants.COMPANY_ID);
    }

    public void responseFriendRequest(int result) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("RMF|" + result);
    }

    public boolean isConnect() {
        if (dataTransfer == null) {
            return false;
        }
        return dataTransfer.isConnect();
    }

    public void eatCardPhom(int playeEatId, int cardId, int anChot) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("EA|" + playeEatId + "|" + cardId + "|" + anChot);
    }

    public void haphom(int playerId, int[] phom, int phomLength) {
        if (dataTransfer == null) {
            return;
        }

        StringBuilder msg = new StringBuilder();
        msg.append("HP|");
        msg.append(playerId);
        msg.append("|");
        for (int i = 0; i < phomLength; i++) {
            msg.append(phom[i]);
            if (i != phomLength - 1) {
                msg.append("|");
            }
        }
        dataTransfer.request(msg.toString());
    }

    public void finishBacay() {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("CT|");
    }

    public void changeGameRule(int rule) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("GM|" + rule);
    }

    public void changeReadyState(int state) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("RE|" + state);
    }

    public void winUPhom(int type) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("U|" + type);
    }

    public void getCardDraw() {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("NB|");
    }

    public void finishTienlen() {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("CT|");
    }

    public void finishPhom() {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("CT|0");
    }

    public void exitChooseRoom() {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("RGA|");
    }

    public void exitTable() {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("RRO|");
    }

    public void exitChoooseTable() {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("RSE|");
    }

    public void finishGame(int turn) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("FINISH|" + turn);
    }

    public void joinGame(String name) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("JOIN|" + name);

    }

    public void checkMate(int id) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("CHECKMATE|" + id + "|" + (1 - id));;
    }

    public void movePiece(int id, int srcRow, int srcColumn, int desRow, int desColumn) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("STEP|" + id + "|" + srcRow + "|" + srcColumn + "|" + desRow + "|" + desColumn);
    }

    public void enterRoom(int roomId) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("SE|" + roomId);
    }

    public void enterTable(int tableId) {
        if (dataTransfer == null) {
            return;
        }
        Debug.d("table choose :" + tableId);
        dataTransfer.request("RO|" + tableId);
    }

//    public void exitServer() {
//        if (dataTransfer == null) {
//            return;
//        }
//        dataTransfer.request("Q");
//    }
    public void fightCardPhom(int playerId, int cardId) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("DB|" + playerId + "|" + cardId);
    }

    public void finishOpenCardBaCay() {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("FN|");
    }

    public void fightCardTienlen(int playerId, int lastplayerFightId, int blockCardType, int[] fightCardArr) {
        if (dataTransfer == null) {
            return;
        }
        StringBuilder msg = new StringBuilder();
        msg.append("DB2|");
        msg.append(playerId);
        msg.append("|");
        msg.append(lastplayerFightId);
        msg.append("|");
        msg.append(blockCardType);
        msg.append("|");
        for (int i = 0, l = fightCardArr.length; i < l; i++) {
            msg.append(fightCardArr[i]);
            if (i != l - 1) {
                msg.append("|");
            }
        }
        Debug.d("msg :" + msg.toString());
        dataTransfer.request(msg.toString());
    }

    public void skipTurnTienlen(int id) {
        dataTransfer.request("BL|" + id);
    }

    public void tienlenFn2(int gameResult, int numberPlayerStart, int playerId) {
        dataTransfer.request("FN2|" + gameResult + "|" + numberPlayerStart + "|" + playerId);
    }

    public void tienlenFn3(int gameResult, int numberPlayerStart, int playerId) {
        dataTransfer.request("FN3|" + gameResult + "|" + numberPlayerStart + "|" + playerId);
    }

    public void finishChessGame(int winID) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("CT|" + winID);
    }

    public void enterBattle(int playerId, int oldRowKing, int oldColumKing, int newRowKing,
            int newColumnKing, int oldRowRock, int oldColumnRock, int newRowRock, int newColumRock) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("EB|" + playerId + "|" + oldRowKing + "|" + oldColumKing + "|" + newRowKing + "|"
                + newColumnKing + "|" + oldRowRock + "|" + oldColumnRock + "|" + newRowRock + "|" + newColumRock);
    }

    public void betMoney(String moneyBet) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("MS|" + moneyBet);
    }

    public void askForDraw(int id) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("DRAW|" + id);
    }

    public void acceptLose(int id) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("LOSE|" + id);
    }

    public void doneTienlen(int id) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("LOSE|" + id);
    }

    public void promoteChess(int chessId, int row, int column) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("UPG|" + chessId + "|" + row + "|" + column);
    }

    public void kickOut(String userName) {
        Debug.d("kick :" + userName);
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("KICK|" + userName);
    }

    public void chat(String message) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("CHAT|" + message);
    }

    public void request(String msg) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request(msg);
    }

    public void nextTurnChess() {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("NEXT|");
    }

    public void reponseDrawAsk(int result) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("AD|" + result);
    }

    public void sendFriendRequest(int playerId, int friendId) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("MF|" + playerId + "|" + friendId);
    }

    public void sendDepositCard(int fromId, int toId, int cardId, int posType) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("GUI|" + fromId + "|" + toId + "|" + cardId + "|" + posType);
    }

    public void sendLuckCoinCredit(String dataCredit) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("XENG|" + dataCredit);
    }

    void getUserInfo() {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("INFO|");
    }

    public void sendMessage(int kind, String userReceive, String message) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("MSG|" + userReceive + "|" + kind + "|" + message);
    }

    void getUserInfo(String name) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("INFO|" + name);
    }

    void getListFriend() {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("LF|");
    }

    void getListItem() {
        Debug.d("get list item :" + dataTransfer);
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("MYI|");
    }

    void getTopRich() {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("TR|");

    }

    void getTopLevel() {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("TL|");

    }

    void getListMessage() {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("LMSG|");

    }

    void useItem(int itemId) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("API|" + itemId);
    }

    void buyItem(int itemId) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("BUY|" + itemId + "|" + 1);
    }

    void birthChild(String matName) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("BIR|" + matName);
    }

    void nameChild(String childName) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("NAME|" + childName);
    }

    void readMessage(int indexSelected) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("RMSG|" + indexSelected);
    }

    void deletedMessage(int indexSelected) {
        if (dataTransfer == null) {
            return;
        }
        dataTransfer.request("DMSG|" + 1 + "|" + indexSelected);
    }

    void deletedAllMessage(int number, int[] id) {
        if (dataTransfer == null) {
            return;
        }
        StringBuffer msg = new StringBuffer();
        msg.append("DMSG|").append(number);
        for (int i = 0; i < number; i++) {
            msg.append("|").append(id[i]);
        }
        dataTransfer.request(msg.toString());
    }

    public void ping(int id) {
        dataTransfer.request("PING|" + id);
    }

    public void ping() {
        dataTransfer.request("PING|");
    }

    public void setMoney(int numberMoney) {
        dataTransfer.request("SETMONEY|" + numberMoney);
    }

    public void updateMoney() {
        dataTransfer.request("UMO|");
    }

    void chargeSms(String serviceCode, String userCharge) {
        dataTransfer.request("SMS|" + serviceCode + "|" + userCharge);
    }
}
